var svc, res, baseurl, layerid;
var iw = new google.maps.InfoWindow();
var WMSlink = {
    "Pipe_in_Plan": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [7]},
    "electricityPipe": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [3, 4, 5, 6]},
    "wms_water_praya": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [0, 1, 2]},
    "wms_Pipeline_age_10": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [8]},
    "wms_Main_pipe_age_10": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [12]},
    "wms_distribution_pipe_age_10": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [16]},
    "wms_pipe_AC_age_10": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [20]},
    "wms_Pipe_line_age_10_to_20": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [9]},
    "wms_Main_Pipe_age_10_to_20": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [13]},
    "wms_distribution_pipe_age_10_to_20": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [17]},
    "wms_pipe_AC_age_10_to_20": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [21]},
    "wms_pipe_line_age_20_to_30": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [10]},
    "wms_Main_pipe_age_20_to_30": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [14]},
    "wms_distribution_pipe_age_20_to_30": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [18]},
    "wms_pipe_ac_age_20_to_30": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [22]},
    "wms_pipe_line_age_30_to_40": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [11]},
    "wms_Main_pipe_age_30_to_40": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [15]},
    "wms_distribution_pipe_age_30_to_40": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [19]},
    "wms_pipe_ac_age_30_to_40": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [23]},
    "wms_lup_2556": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [0, 1, 2]},
    "wms_tunnel_originel": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [29]},
    "wms_main_pipe_originel": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [31]},
    "wms_distribution_pipe": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [30]},
    "wms_region": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [26, 27]},
    "gps_av": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/engineer/MapServer", "layerIds": [28]},
    "stolenmeter_2014_08": {"baseurl": "", "layerIds": []},
    "stolenmeter_2014_09": {"baseurl": "", "layerIds": []},
    "stolenmeter_2014_10": {"baseurl": "", "layerIds": []},
    "stolenmeter_2014_11": {"baseurl": "", "layerIds": []},
    "stolenmeter_2014_12": {"baseurl": "", "layerIds": []},
    "stolenmeter_2015_01": {"baseurl": "", "layerIds": []},
    "stolenmeter_2015_02": {"baseurl": "", "layerIds": []},
     "stolenmeter_2015_03": {"baseurl": "", "layerIds": []},
      "stolenmeter_2015_04": {"baseurl": "", "layerIds": []},
       "stolenmeter_2015_05": {"baseurl": "", "layerIds": []},
        "stolenmeter_2015_06": {"baseurl": "", "layerIds": []},
    "Customers_in_DMA": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Service/MapServer", "layerIds": [8]},
    "Valve_in_DMA": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Service/MapServer", "layerIds": [9]},
    "Trunkmain_leak_DMA": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Service/MapServer", "layerIds": [10]},
    "servicearea": {"baseurl": "", "layerIds": []},
    "police": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Service/MapServer", "layerIds": [12]},
    "effectivearea": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/effective_area/MapServer", "layerIds": [0,1]},
    "branches": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Service/MapServer", "layerIds": [7]},
    
    
    "repeatedFlood": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Water_resource/MapServer", "layerIds": [4]},
    "repeatedDrought": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Water_resource/MapServer", "layerIds": [5]},
    "River-basin": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Water_resource/MapServer", "layerIds": [6]},
    "DataEarth": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Water_resource/MapServer", "layerIds": [7, 8, 9]},
    "well": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Water_resource/MapServer", "layerIds": [0, 1, 2, 3]},
    "MRT_project": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/future/MapServer", "layerIds": [2]},
    "newdma": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/future/MapServer", "layerIds": [3]},
    "project9": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/future/MapServer", "layerIds": [0, 1]},
    
    "waterquality": {"baseurl": "", "layerIds": []},
    "resource": {"baseurl": "", "layerIds": []},
    
    "wlma_status": {"baseurl": "WLMA", "layerIds": []},
    "wlma_NFA_Alert": {"baseurl": "WLMA", "layerIds": []},
    "wlma_p_wl": {"baseurl": "WLMA", "layerIds": []},
    "wlma_gpa_av": {"baseurl": "WLMA", "layerIds": []},
    "wlma_RTU_Status": {"baseurl": "WLMA", "layerIds": []},
    "wlma_DMA_LAST": {"baseurl": "WLMA", "layerIds": []},
    "wlma_DMA_YEAR": {"baseurl": "WLMA", "layerIds": []},
    "wlma_P_AVG": {"baseurl": "WLMA", "layerIds": []},
    "wlma_LAST_LD": {"baseurl": "WLMA", "layerIds": []},
    "wlma_LAST_LR": {"baseurl": "WLMA", "layerIds": []},
    "landuse": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/Future/MapServer", "layerIds": [5,6,7]},
    "wms_crm1": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/service_for_latlng/MapServer", "layerIds": [2]},
    "wms_crm2": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/service_for_latlng/MapServer", "layerIds": [3]},
    "wms_crm3": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/service_for_latlng/MapServer", "layerIds": [4]},
    "wms_crm4": {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/service_for_latlng/MapServer", "layerIds": [5]}
};
function getFeatureInfo(latLng) {
    if (toplevel > 0)
    {

        baseurl = WMSlink[wms_order[toplevel - 1]].baseurl;
        layerid = WMSlink[wms_order[toplevel - 1]].layerIds;
        //console.log(layerid);
        if (baseurl == "WLMA")
            identify_WLMA(latLng);
        else if (baseurl != "")
            identify(latLng, layerid, baseurl);

    }
}

function identify(latLng, layerid, baseurl) {
    console.log(baseurl);
    if (res)
        res.length = 0;
    svc = new gmaps.ags.MapService(baseurl);
    svc.identify({
        'geometry': latLng,
        'tolerance': 10,
        'layerIds': layerid,
        //'layerOption': 'all',
        'bounds': map.getBounds(),
        'width': map.getDiv().offsetWidth,
        'height': map.getDiv().offsetHeight,
        'maxAllowableOffset': 3
    }, function (results, err) {
        if (err) {
            alert(err.message + err.details.join('\n'));
        } else {
            //console.log(baseurl, layerid, latLng, results);
            addResultToMap(results, latLng);
        }
    });
}
function addResultToMap(idresults, latlng) {
    res = idresults.results;
    console.log(idresults);
    var content = "";
    if (res.length == 0)
    {
        //content = "ไม่พบข้อมูล";
    }
    else {
        //for (var i = 0; i < res.length; i++) {
        // display only the first result
        for (var i = 0; i < 1; i++) {
            layername = res[i].layerName;
            content += "<h3>" + layername + "</h3>";
            var value = res[i].feature.attributes;
            for (var attributes in value)
            {
                content += "<b>" + attributes + "</b> : ";
                // สร้าง href สำหรับ link
                var link_pattern = new RegExp("^https?://");
                if (link_pattern.test(value[attributes]))
                    content += "<a href = '" + value[attributes] + "' target='_blank'>" + attributes + "</a><br>";
                // else 
                else
                    content += value[attributes] + "<br>";
            }
        }
        //console.log(content);
        iw.close();
        iw = new google.maps.InfoWindow({
            content: content,
            position: latlng,
            maxWidth: 270
        });
        iw.open(map);
    }


}
function identify_WLMA(latlng) {

    //"wms_wlma_all" : {"baseurl": "http://172.16.195.26/arcgis/rest/services/dss/WLMA/MapServer", "layerIds": [18]
    var convertToWGS84 = toMercator(latlng.B, latlng.k);
    var BLOCKNAME = null;
    $.getJSON("rest/query_wlma.php", {x: convertToWGS84[0], y: convertToWGS84[1]}, function (data, status, xhr) {
        $.each(data, function (key, value) {
            $.each(value, function (key, values) {
                 //console.log(values);
                 BLOCKNAME = values.attributes.BLOCKNAME;
            });
           
            //BLOCKNAME = value.attributes.BLOCKNAME;
        });
        $.get("rest/soap_wlma.php", {dma: BLOCKNAME}, function (data, status, xhr) {
            //console.log("soap = ", data);
           
            //$(this).html("Hello <b>world</b>!");
            var content ;
            $(data).each(function(key, result){
                //console.log("dataa = ", result);
                $(result).each(function(key, results){
                    //console.log("results = ", results);
                     var split = $(this).find("table").html();    
                     //console.log(split);
                     content  = split;
                });
            });
            $('table.wlma').css({"font-size" : "1pt"});
            var html = "<table class='wlma' style='font-size : 10pt;'>"+content+"</table>"
            iw.close();
            iw = new google.maps.InfoWindow({
                content: html,
                position: latlng,
                maxWidth: 3000,
                maxHeight : 2500
            });
            if(data.length >=70)
                iw.open(map);
        });
    });

}
function toMercator(lon, lat) {
    var x = lon * 20037508.34 / 180;
    var y = Math.log(Math.tan((90 + lat) * Math.PI / 360)) / (Math.PI / 180);
    y = y * 20037508.34 / 180;

    return [x, y];
}
